package com.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.pojo.LoveCar;
import com.pojo.Result;
import com.service.LoveCarService;
import com.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.Path;
import java.util.List;

/**
 * @author ZhaoJiaDong
 * @version 2020.1
 * @date 2022/11/30 0030 14:26
 */
@RestController
@RequestMapping("/loveCar")
@CrossOrigin
public class LoveCarController {
    @Autowired
    private LoveCarService loveCarService;
    @Autowired
    private OssService ossService;
    private String url="";
    @GetMapping("/{username}")
    public Result getCarList(@PathVariable String username){
      List<LoveCar> list = loveCarService.findByUsername(username);
        Result result = new Result();
        if(list!=null){
            result.setMsg("查询成功");
            result.setCode(200);
            result.setData(list);
        }else{
            result.setMsg("查询失败");
            result.setCode(500);
        }
        return result;
    }

    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        boolean flag=loveCarService.deleteById(id);
        Result result = new Result();
        if(flag){
            result.setMsg("删除成功");
            result.setCode(200);
        }else{
            result.setMsg("删除失败");
            result.setCode(500);
        }
        return result;
    }

    @PutMapping("/{id}/{username}")
    public Result updateById(@PathVariable Integer id,@PathVariable String username){
         LoveCar car =loveCarService.updateById(id,username);
        Result result = new Result();
        if(car!=null){
            result.setMsg("修改成功");
            result.setCode(200);
            result.setData(car);
        }else{
            result.setMsg("修改失败");
            result.setCode(500);
        }
        return result;
    }

    @PostMapping("/updateImgById/{id}")
    public Result updateImgById(Integer id,String img){
        boolean flag= loveCarService.updateImgById(id,img);
        Result result = new Result();
        if(flag){
            result.setMsg("修改成功");
            result.setCode(200);
        }else{
            result.setMsg("修改失败");
            result.setCode(500);
        }
        return result;
    }

    @PostMapping("/getImageUrl")
    public R uploadOssFile(MultipartFile file){
        System.out.println("file="+file.getOriginalFilename());
        this.url = ossService.uploadFileAvatar(file);
        System.out.println("实打实大卧底啊欧弟脑袋那我ID加我就打我基底哦"+url);
        return R.ok(url).setCode(20000);
    }

    @GetMapping("/{username}/{newNumber}")
    public Result add(@PathVariable String username,@PathVariable String newNumber){
        boolean flag= loveCarService.add(username,newNumber);
        Result result = new Result();
        if(flag){
            result.setMsg("修改成功");
            result.setCode(200);
        }else{
            result.setMsg("修改失败");
            result.setCode(500);
        }
        return result;
    }
}
